


//
//  ControlsViewController.swift
//  Swift 3.0 基础语法
//
//  Created by YYY on 2017/7/17.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class ControlsViewController: CommonViewController {


 
    var numberField1: UITextField?

    var numberField2: UITextField?
    
    var mySumLabel: UILabel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //UIButton创建
        let btn = UIButton(frame: CGRect(x: 100, y: 300, width: 100, height: 25))
        btn.setTitle("点击", for: .normal)
        btn.setTitleColor(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1), for: .normal)
        btn.backgroundColor = UIColor.gray
        view.addSubview(btn)
        
        test(x: nil, y: nil)
        
        //加法计算器
        let textField1 = UITextField(frame: CGRect(x: 30, y: 70, width: 100, height: 30), placeholder: "0")
        view.addSubview(textField1)
        
        let label1 = UILabel(frame: CGRect(x: 130, y: 70, width: 50, height: 30))
        label1.textAlignment = .center
        label1.text = "+"
        view.addSubview(label1)
        
        let textField2 = UITextField(frame: CGRect(x: 180, y: 70, width: 100, height: 30), placeholder: "0", fontSize: 14)
     
        view.addSubview(textField2)
        
        numberField1 = textField1
        numberField2 = textField2
        
        let result = UIButton(frame: CGRect(x: 300, y: 70, width: 50, height: 30),title: "=", color: .red)
               
        result.addTarget(self, action: #selector(resultAction), for: .touchUpInside)
        result.setTitle("=", for: .normal)
        result.setTitleColor(UIColor.black, for: .normal)
        view.addSubview(result)
        

        let sumLabel = UILabel(frame: CGRect(x: 260, y: 130, width: 100, height: 30))
        sumLabel.layer.borderColor = UIColor.green.cgColor
        sumLabel.layer.borderWidth = 0.5
        sumLabel.text = "0"
        sumLabel.textAlignment = .center
        sumLabel.textColor = UIColor.black
        view.addSubview(sumLabel)
        
        mySumLabel = sumLabel
        // Do any additional setup after loading the view.
    }
    
    func resultAction()
    {
        print(numberField1?.text ?? "")
        print("---")
        print(numberField2?.text ?? "")
        
        guard let num1 = Int(numberField1?.text ?? ""),let num2 = Int(numberField2?.text ?? "") else
        {
            print("输入的非数字或者为空")
            return
        }
        
//        guard let num1 = Int((numberField1?.text)!),let num2 = Int((numberField2?.text)!) else
//        {
//            print("输入的非数字或者为空")
//            return
//        }
    
        mySumLabel?.text = "\(num1 + num2)"
        
    }
    
    func test(x:Int?,y:Int?)
    {
        var sum = 0
        guard let x = x,let y = y else {
            print("nil 值")
            return
        }
       
        //sum = x! + y!
        sum = x + y
        print(sum)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
